package com.littlebee.storychild.common;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

import com.littlebee.storychild.common.utils.CrashHandler;
import com.littlebee.storychild.common.utils.NetWorkUtils;
import com.littlebee.storychild.common.utils.Utils;
import com.umeng.analytics.MobclickAgent;
import com.zss.library.utils.LogUtils;

import org.xutils.BuildConfig;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.ContactNotifyEvent;

/**
 * 作者：liaoyong on 2016/10/25 09:51
 * 邮箱：liaoyong@tuliu.com
 * 描述：
 */
public class BaseApplication extends Application {
    private static BaseApplication instance;

    public static BaseApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
        //x.Ext.setDebug(true);
        initCrashConfig(getApplicationContext());

        Log.i("IMDebugApplication", "init");
        JMessageClient.setDebugMode(true);
        JMessageClient.init(getApplicationContext(), true);
        JMessageClient.setNotificationFlag(JMessageClient.FLAG_NOTIFY_WITH_SOUND);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        initPushConfig();
        initUmentConfig();
    }
    /**
     * @Description: 初始化崩溃信息抓取配置
     * @author  liaoyong
     * */
    private void initCrashConfig(Context context) {
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(context, this);
    }

    private void initPushConfig() {
        JPushInterface.setDebugMode(true);
        JPushInterface.init(getApplicationContext());
    }

    private void initUmentConfig() {
        MobclickAgent.setDebugMode(true);
        // SDK在统计Fragment时，需要关闭Activity自带的页面统计，
        // 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);
    }

    public void sendReportEvent(String eventType, String eventCode) {
        if (Utils.getUserEntry() == null)
            return;
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("deviceid", Utils.getUserEntry().getMobile());
        hashMap.put("eventtype", eventType);
        hashMap.put("eventcode", eventCode);
        NetWorkUtils.post(getApplicationContext(), "babyreport", hashMap, null);
    }
}
